<template>
	<view class="template-edit tn-safe-area-inset-bottom">
		<!-- 文字生成图片 -->
		<tn-nav-bar fixed alpha customBack>
		  <view slot="back" class='tn-custom-nav-bar__back'
		    @click="goBack">
		    <text class='icon tn-icon-left'></text>
		    <text class='icon tn-icon-home-capsule-fill'></text>
		  </view>
			<view class="tn-flex tn-flex-col-center tn-flex-row-center ">
			  <text class="tn-text-bold tn-text-xl tn-color-black"></text>
			</view>
		</tn-nav-bar>

		<template v-if="show == 1">
			<view class="tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">
				<!-- 图片上传开始 -->
				<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
					<view class="tn-flex justify-content-item">
						<view class="tn-bg-black tn-color-white tn-text-center"
							style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
							<text class="tn-icon-image" style="font-size: 30rpx;"></text>
						</view>
						<view class="tn-text-lg tn-padding-right-xs tn-text-bold">上传图片 *</view>
					</view>
					<view class="justify-content-item tn-text-df tn-color-grey">
						<text class="tn-padding-xs">请上传合适的图片</text>
						<text class="tn-icon-baby"></text>
					</view>
				</view>

				<view class="tn-margin-left tn-padding-top-xs"  v-if="accountStae">
					<tn-image-upload-drag
					  ref="imageUpload"
					  :action="action"
					  :width="710"
					  :height="500"
					  :formData="formData"
					  :fileList="fileList"
					  :disabled="disabled"
					  :autoUpload="autoUpload"
					  :maxCount="maxCount"
					  :showUploadList="showUploadList"
					  :showProgress="showProgress"
					  :deleteable="deleteable"
					  :customBtn="customBtn"
					  @sort-list="onSortList"
						:header="header"
					/>
				</view>
				<!-- 图片上传结束 -->
				
				<!-- 图片识别结果开始 -->
				<view class="">
					<template v-for="(item, index) in dataList">
						<tn-list-view :title="`识别结果` + (index + 1 )" :card="true" unlined="all">
							<block v-for="(value, key) in item" :key="index">
								<tn-list-cell>{{value.key}}: {{value.value}}</tn-list-cell>
							</block>
						</tn-list-view>
					</template>
				</view>
				<!-- 图片识别结果结束 -->

				<!-- 悬浮按钮-->
				<view class="tn-flex tn-footerfixed" v-if="accountStae">
					<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
						<tn-button backgroundColor="#3668fc" padding="40rpx 0" width="70%" fontBold @click="submit">
							<!-- <text class="tn-icon-light tn-padding-right-xs tn-color-black"></text> -->
							<text class="tn-color-white">立 即 识 别</text>
							<!-- <text class="tn-icon-camera tn-padding-left-xs tn-color-black"></text> -->
						</tn-button>
						<view class="tn-padding-xs tn-text-sm">
							<text class="tn-icon-tip-fill tn-color-gray tn-padding-right-xs"></text>
							<text class="tn-color-gray">消耗 -100 积分</text>
						</view>
					</view>
				</view>

			</view>
		</template>

		<view class='tn-tabbar-height'></view>

	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	import { authorState,publishWallPaper} from "@/utils/api/author.js"
	import { configState } from "@/utils/api/sys.js"
	import { baiduRecognition } from "@/utils/api/ai.js"
	export default {
		name: 'TemplateEdit',
		mixins: [template_page_mixin],
		data() {
			return {
				action: getApp().globalData.baseUrl + "author/file/upload",
				formData: {},
				header: {
					Authorization: 'Bearer ' + uni.getStorageSync("login"),
				},
				showUploadList: true,
				customBtn: true,
				autoUpload: true,
				showProgress: true,
				deleteable: true,
				customStyle: false,
				maxCount: 1,
				disabled: false,
				show: 2,
				accountStae: true,
				sence: "",
				dataList: [],
			}
		},
		onLoad(params) {
			this.sence = params.code
		},
		onShow() {
			this.getSysState()
		},
		methods: {
			getSysState () {
				configState({key: "wechat_mini_state"}).then(res => {
					this.show = res.val
					if (res.val == 1) {
						this.getAuthorState()
					}
				})
			},
			// 检测认证状态
			getAuthorState() {
				authorState().then(res => {
						if (res.code == 1) {
							if (res.info.audit_status == 3) {
								this.$func.showToast("认证审核中 请耐心等待")
								this.accountStae = false
								return
							}
							if (res.info.audit_status == 2) {
								this.accountStae = false
								uni.showModal({
									title: '认证提示',
									content: "认证已驳回 请重新认证",
									cancelText: "暂不认证",
									confirmText: "重新认证",
									success: function (res) {
										if (res.confirm) {
											uni.navigateTo({
												url: "/pageA/author/ident"
											})
										}
									}
								})
								return
							}
							if (res.info.is_forbidden == 1) {
								this.accountStae = false
								uni.showModal({
									title: '账号提示',
									content: "账号被禁用 联系客服",
									cancelText: "暂无联系",
									confirmText: "立即联系",
									success: function (res) {
										if (res.confirm) {
											uni.navigateTo({
												url: "/pageA/help/help"
											})
										}
									}
								})
								return
							}
						} else {
							uni.showModal({
								title: '认证提示',
								content: "未认证 请先认证",
								cancelText: "暂不认证",
								confirmText: "立即认证",
								success: function (res) {
									if (res.confirm) {
										uni.navigateTo({
											url: "/pageA/author/ident"
										})
									}
								}
							})
						}
				})
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				})
			},
			// 提交表单
			submit() {
				let files = '';
				let fileList = this.$refs.imageUpload.lists
				for (let i = 0; i < fileList.length; i++) {
					files = fileList[i].data.response.data.url
				}
				if (this.sence == "") {
					this.$func.showToast("识别场景不存在")
					return
				}
				let postParams = {
					scene: this.sence,
					imageUrl: files,
				}
				uni.showLoading({
					title: '提交中',
					mask: true
				})
				baiduRecognition(postParams).then(res => {
					uni.hideLoading()
					if (res.code == 0) {
						// this.$refs.imageUpload.clear(),
						this.$func.showToast(res.msg)
						this.dataList = res.data
					} else {
						this.$func.showToast(res.msg)
					}
				})
			},
			// 图片拖拽重新排序
			onSortList(list) {
				console.log(list);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.template-edit {}
	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 180rpx;
		height: calc(220rpx + env(safe-area-inset-bottom) / 2);
		height: calc(220rpx + constant(safe-area-inset-bottom));
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);

	}

	/* 底部悬浮按钮 end*/

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/
</style>